/*****************************************************************************************
Arise Engine		ClipBuffer.h

					E-mail:		arise_engine@mail.ru

					Origin by:	DK Engine 0.019,
								KAIN (igor@donec.net), Agressor (agress0r@mail.ru)

created:			13.03.2005
last changes:		21.04.2007
*****************************************************************************************/
#pragma once

#include <d3dx9.h>
#include <vector>


class aClipBuffer
{
private:

	UINT m_size;

public:

	D3DXVECTOR3* m_buffer;

	aClipBuffer(UINT size)
	{
		m_size		= size;
		m_buffer	= 0;
		
		if(m_size <= 0) return;
		
		m_buffer = new D3DXVECTOR3[m_size];
	}
	~aClipBuffer()
	{
		SAFE_DELETE_ARRAY(m_buffer);
	}

	void Release()
	{
		SAFE_DELETE_ARRAY(m_buffer);
	}
};



class ClipManager
{
private:

public:
	ClipManager()
	{
		m_counter = -1;
	}
	~ClipManager()
	{
		if(!m_elements.empty())
		{
			for(	std::vector<aClipBuffer*>::iterator it = m_elements.begin(); 
					it != m_elements.end();
					it++)
			{
					(*it)->Release();
					delete (*it);
			}
		}		
		m_elements.clear();
	}


	int m_counter;
	std::vector<aClipBuffer*> m_elements;


	void Init(UINT num_elements, UINT buffer_size)
	{
		m_elements.resize(num_elements);

		for(	std::vector<aClipBuffer*>::iterator it = m_elements.begin(); 
				it != m_elements.end();
				it++)
		{
				(*it) = new aClipBuffer(buffer_size);
		}
	}


	aClipBuffer* GetRegularBuffer()
	{
		m_counter++;
		if(m_counter == m_elements.size()) m_counter = 0;

		return m_elements[m_counter];
	}


	void Release()
	{
		if(!m_elements.empty())
		{
			for(	std::vector<aClipBuffer*>::iterator it = m_elements.begin(); 
					it != m_elements.end();
					it++)
			{
					if((*it))
					{
						(*it)->Release();
						delete (*it);
					}
			}
		}		
		m_elements.clear();
	}

};




